package com.proscenic.ops.mp.handle;

import com.proscenic.ops.mp.domain.MpMedia;
import com.proscenic.ops.mp.mapper.MpMediaMapper;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.builder.outxml.TextBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author Binary Wang
 */
@Component
@Slf4j
public class SubscribeHandler implements WxMpMessageHandler {
    @Resource
    private MpMediaMapper mpMediaMapper;

    @Override
    public WxMpXmlOutTextMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService,
                                        WxSessionManager sessionManager) throws WxErrorException {

        log.info("新关注用户 OPENID: " + wxMessage.getFromUser());

        // 获取微信用户基本信息
        WxMpUser userWxInfo = wxMpService.getUserService().userInfo(wxMessage.getFromUser(), null);

        log.info("用户信息：" + userWxInfo);

        //文字
        WxMpKefuMessage message = WxMpKefuMessage.TEXT().toUser(wxMessage.getFromUser()).content("感谢你的关注").build();
        wxMpService.getKefuService().sendKefuMessage(message);

        //海报
        MpMedia media = mpMediaMapper.selectById(1);
        message = WxMpKefuMessage.IMAGE().toUser(wxMessage.getFromUser()).mediaId(media.getMediaId()).build();
        wxMpService.getKefuService().sendKefuMessage(message);

        //企业号
//        media = mpMediaMapper.selectById(2);
//        WxMpKefuMessage.IMAGE().toUser(wxMessage.getFromUser()).mediaId(media.getMediaId()).build();

        return null;
    }

}
